package com.university.evaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.university.evaluation.entity.EvaluationItem;
import com.university.evaluation.mapper.EvaluationItemMapper;
import com.university.evaluation.service.EvaluationItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
// 评价项服务实现类
public class EvaluationItemServiceImpl extends ServiceImpl<EvaluationItemMapper, EvaluationItem> implements EvaluationItemService {

    @Autowired
    private EvaluationItemMapper evaluationItemMapper;

    @Override
    public List<EvaluationItem> findBySystemId(Long systemId) {
        return evaluationItemMapper.findBySystemId(systemId);
    }

    @Override
    public List<EvaluationItem> findModulesBySystemId(Long systemId) {
        return evaluationItemMapper.findModulesBySystemId(systemId);
    }

    @Override
    public List<EvaluationItem> findItemsByParentId(Long parentId) {
        return evaluationItemMapper.findItemsByParentId(parentId);
    }

    @Override
    public boolean setItemWeight(Long itemId, BigDecimal weight) {
        EvaluationItem item = getById(itemId);
        if (item != null) {
            item.setWeight(weight);
            item.setUpdateTime(LocalDateTime.now());
            return updateById(item);
        }
        return false;
    }

    @Override
    public boolean updateScoreRule(Long itemId, String scoreRule) {
        EvaluationItem item = getById(itemId);
        if (item != null) {
            item.setScoreRule(scoreRule);
            item.setUpdateTime(LocalDateTime.now());
            return updateById(item);
        }
        return false;
    }

    @Override
    public List<EvaluationItem> batchSave(List<EvaluationItem> itemList) {
        LocalDateTime now = LocalDateTime.now();
        for (EvaluationItem item : itemList) {
            item.setUpdateTime(now);
            if (item.getId() == null) {
                item.setCreateTime(now);
                save(item);
            } else {
                updateById(item);
            }
        }
        return itemList;
    }

    @Override
    public List<EvaluationItem> findByItemType(String itemType) {
        QueryWrapper<EvaluationItem> wrapper = new QueryWrapper<>();
        wrapper.eq("item_type", itemType);
        return list(wrapper);
    }

    @Override
    public List<EvaluationItem> findBySystemIdAndParentId(Long systemId, Long parentId){
        return evaluationItemMapper.findBySystemIdAndParentId(systemId, parentId);
    }

}